package com.bwie.controller;

import com.bwie.common.domain.Dict;
import com.bwie.common.domain.Literature;
import com.bwie.common.domain.request.LiteratureListReq;
import com.bwie.common.domain.response.LiteratureListResp;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.service.LiteratureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @className: LiteratureController
 * @Description: TODO
 * @version: v1.８.0
 * @author: 李明
 * @date: 2025/11/10 10:32
 */
@RestController
@RequestMapping("literature")
@CrossOrigin
public class LiteratureController {

    @Autowired
    private LiteratureService literatureService;

    /**
     * 资源列表展示、分页无问题（3分）
     */
    @PostMapping("literatureList")
    public Result<PageResult<LiteratureListResp>> literatureList(@RequestBody LiteratureListReq literatureListReq){
        PageHelper.startPage(literatureListReq.getPageNum(), literatureListReq.getPageSize());
        List<LiteratureListResp> literatureListRespList =
                literatureService.literatureList(literatureListReq);
        PageInfo<LiteratureListResp> literatureListRespPageInfo =
                new PageInfo<>(literatureListRespList);
        //分页
        return PageResult.toResult(literatureListRespPageInfo.getTotal(), literatureListRespList);
    }


    /**
     * 上传文件
     */
    @PostMapping("importLiteratureData")
    public Result<List<Literature>> importLiteratureData(
            @RequestParam("file") MultipartFile multipartFile){
        List<Literature> literatureList = literatureService.importLiteratureData(multipartFile);
        return Result.success(literatureList);
    }


    /**
     * 下拉框
     */
    @GetMapping("selectDictValue/{dictType}")
    public Result<List<Dict>> selectDictValue(@PathVariable("dictType") String dictType){
        List<Dict> dictList = literatureService.selectDictValue(dictType);
        return Result.success(dictList);
    }


    /**
     * 资源列表展示-查重
     */
    @PostMapping("getLiteraturePage")
    public Result<PageResult<Literature>> getLiteraturePage(@RequestBody LiteratureListReq literatureListReq){
        PageHelper.startPage(literatureListReq.getPageNum(), literatureListReq.getPageSize());
        List<Literature> literatureList = literatureService.getLiteraturePage(literatureListReq);
        PageInfo<Literature> literaturePageInfo = new PageInfo<>(literatureList);
        return PageResult.toResult(literaturePageInfo.getTotal(), literatureList);
    }

}
